/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipinyou.wordstatistics.listener;

import com.ipinyou.wordstatistics.core.HandleResult;
import com.ipinyou.wordstatistics.core.HandleResult.CodeInfo;
import com.ipinyou.wordstatistics.core.IHandleDoneListener;
import com.ipinyou.wordstatistics.util.LoggerUtil;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 *
 * @author xiangnan.wang@ipinyou.com
 */
public class ExportxlsListener implements IHandleDoneListener{

    //文件的路径
    private String filePath="./default_xls.xls";
    
    private int sidx=0;
    
    private String sname;
    
    public ExportxlsListener(String path,int sidx,String sname){
        this.filePath=path;
        this.sidx=sidx;
        this.sname=sname;
    }
    
    public void handle(List<HandleResult> results) {
        try { 
            POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(filePath));
            HSSFWorkbook wb=new HSSFWorkbook(fs);
            int tmpIdx=wb.getSheetIndex(sname);
            if(tmpIdx>=0){
                wb.removeSheetAt(tmpIdx);
            }
            int sheetNum=wb.getNumberOfSheets();
            while((sheetNum-1)<sidx){
                wb.createSheet();
                sheetNum++;
            }
            HSSFSheet sheet=wb.getSheetAt(sidx);
            if(sheet!=null){
                wb.removeSheetAt(sidx);
            }
            sheet=wb.createSheet(sname);
            wb.setSheetOrder(sname, sidx);
            sheet.setFitToPage(true);
            sheet.autoSizeColumn(0);
            sheet.setColumnWidth(1, 60*256);
//            sheet.autoSizeColumn(1,true);
            int rn=0;
            CellStyle style=wb.createCellStyle();
//            style.setFillBackgroundColor(IndexedColors.YELLOW.index);
            style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
            CellStyle style1=wb.createCellStyle();
            style1.setWrapText(true);
            for(int i=0;i<results.size();i++){
                HandleResult result=results.get(i);
                HSSFCell fileCell=sheet.createRow(rn).createCell(0);
                fileCell.setCellStyle(style);
                fileCell.setCellValue(result.getFileName());
                sheet.addMergedRegion(new CellRangeAddress(rn,rn,0,1));
                rn++;
                List<CodeInfo> cil=result.getCodeInfoL();
                for(CodeInfo ci:cil){
                    HSSFRow row=sheet.createRow(rn);
                    row.createCell(0).setCellValue(ci.getLineNum()+"|"+ci.getStartIndex()+"|"+ci.getEndIndex());
                    HSSFCell codeCell=row.createCell(1);
                    codeCell.setCellValue(ci.getLineCode());
                    codeCell.setCellStyle(style1);
                    rn++;
                }
            }
            OutputStream fos = new FileOutputStream(filePath);
            wb.write(fos);
        } catch (IOException ex) {
            LoggerUtil.getLogger().log(Level.SEVERE,"handle error",ex);
        }
    }

}
